package in.falkeninc.umt_v0_9_8_1;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

/**
 * 'Coding a program towards the end is like finishing up solitaire, but instead it gets simpler yet more complicated at the same time. The 80/20 rule is the truth, indeed. - Me '
 * 
 * Extend FragmentActivity since we want to support pre 3.0. This is the only way, so i guess ActionBars are added programatically
 * And to use actionbars pre 30 we need sherlock lib.
 * 
 * Extract dir to short path, excluding eventual @ in dir path!!
 * Import from source, mark as library ->proj->prop->andr->As Library
 * Add project (open) in your project
 * 
 * @author Eivind Falkenstein
 * @version 0.9.8.1
 * @category Main controller class
 *
 */

public class MainActivity extends BaseAlarmActivities   {
	
	// these vars are used when DetailsFragment is plugged into this Activity
	public int medicon = 0;
	public int delicon = 0;	

	public String alarmid = "-1";
	public String alarmName = "No name"; 
	public String alarmDesc = "No description";	
	
	@SuppressWarnings("unused")
	private DatabaseHandler db;
	
	boolean config_frag = false;

	/* 
	 * Sets the view. Depending on orientation it will select either res/layout/fragment_layout.xml (portrait mode) or
	 * res/layout-land/fragment_layout.xml (landscape mode). This is done automatically by the system.
	 * 
	 * (non-Javadoc)
	 * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
	 */
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		Toast.makeText(this, "MainActivity: OnCreate()", Toast.LENGTH_SHORT).show();

		ActionBar actionBar;
		actionBar = getSupportActionBar();
		actionBar.show();
		actionBar.setTitle(R.string.app_name);
		
		db = new DatabaseHandler(this);

		// DNR / internal stuff
//		db.dump_database_to_sdcard();
//		db.dropTable();
		
		
		setContentView(R.layout.fragment_list);

	}
	

	// This is a secondary activity, to show what the user has selected when the
	// screen is not large enough to show it all in one activity.

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		
		// Inflate the menu; this adds items to the action bar if it is present.
		getSupportMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	public void doIt(View v) {
		Log.d("DOIT", "JUST FUCVKING DO IT");
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.action_config:
	        	configclick();
	            return true;
	        case R.id.action_add:
	        	addclick();
	            return true;
	        case R.id.action_about:
	        	aboutclick();
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	public void aboutclick() {
		AboutDialog about = new AboutDialog(this);
		about.setTitle("Universal Multi Timer");
		about.show();
	}
	
	/**
	 * Displays the screen to add a new alarm
	 */
	public void addclick() {
		
		// Toast.makeText(this, "MainActivity: addclick()", Toast.LENGTH_SHORT).show();
		this.alarmid="Multi pane default";
		// Create an intent for starting the DetailsActivity
		Intent intent = new Intent();

		// explicitly set the activity context and class associated with the intent (context, class)
		intent.setClass(this, DetailsActivity.class);

		startActivity(intent);
	}
	
	public void configclick() {
		Log.d("configclick()", String.valueOf(config_frag));
		config_frag ^= true; 
		showConfig();
	}
	
	
	/**
	 * Displays the configuration screen in its own activity with a fragment.
	 */
	void showConfig() {
		
		// Create an intent for starting the DetailsActivity
		Intent intent = new Intent();

		// explicitly set the activity context and class associated with the intent (context, class)
		intent.setClass(this, ConfigActivity.class);

		startActivity(intent);
	}

	// broadcast a custom intent. 
   public void broadcastIntent(View view)
   {
      Intent intent = new Intent();
      intent.setAction("in.falkeninc.umt_v0_9_8_1.CUSTOM_INTENT_ADDALARM");
      sendBroadcast(intent);
   }

}
